package com.itown.expo.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.GrantedAuthority;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import webframe.common.dao.BaseUser;
import webframe.common.helper.ControllerHelper;

import com.itown.expo.controller.base.MyBaseController;
import com.itown.expo.model.CcyRate;
import com.itown.expo.model.Container;
import com.itown.expo.model.ContainerCheck;
import com.itown.expo.model.Decl;
import com.itown.expo.model.DeclGoodsImg;
import com.itown.expo.model.EntUserInfo;
import com.itown.expo.model.EpidemicNotify;
import com.itown.expo.model.Goods;
import com.itown.expo.model.GoodsCheck;
import com.itown.expo.model.GoodsSituation;
import com.itown.expo.model.InspResultSum;
import com.itown.expo.model.MainDeclGoods;
import com.itown.expo.model.MainGoodsDecl;
import com.itown.expo.model.RecordCodes;
import com.itown.expo.model.ReleaseDecl;
import com.itown.expo.model.SupervisePlan;
import com.itown.expo.model.UserLoginInfo;
import com.itown.expo.model.UserRightSpec;
import com.itown.expo.model.UserRightsType;
import com.itown.expo.model.WoodPackCheck;

public class MultiViewController extends MyBaseController {

	public ModelAndView home(HttpServletRequest request,
			HttpServletResponse response) {
		Map home = new HashMap();
		home.put("accessTime", new Date());
		home.put("ip", request.getRemoteAddr());
		return new ModelAndView("Home.jsp", "home", home);
	}

	public ModelAndView menu(HttpServletRequest request,
			HttpServletResponse response) {
		boolean admin = false;
		UserLoginInfo user = (UserLoginInfo) ControllerHelper
				.getUserBySession();
		Map map = new HashMap();
		GrantedAuthority[] authorities = user.getAuthorities();
		for (GrantedAuthority authority : authorities) {
			if (authority.getAuthority().equalsIgnoreCase("ROLE_ADMIN")) {
				admin = true;
			}
		}
		String[] rights = new String[authorities.length];
		int i = 0;
		for (GrantedAuthority grantedAuthority : authorities) {
			String s = grantedAuthority.getAuthority();
			rights[i] = s;
			i++;
		}

		map.put("user", user);
		map.put("rights", rights);
		map.put("admin", admin);
		return new ModelAndView("Menu.jsp", "map", map);
	}

	// //accessdenied
	public ModelAndView accessDenied(HttpServletRequest request,
			HttpServletResponse response) {
		return new ModelAndView("AccessDenied.jsp");
	}

	// //recordCode
	// 创建备案号
	public ModelAndView recordCodeCreate(HttpServletRequest request,
			HttpServletResponse response) {
		RecordCodes r = new RecordCodes();
		Map map = new HashMap();
		map.put("code", r);
		map.put("appPrefix", getAppPrefix());
		return new ModelAndView("RecordCodeEdit.jsp", "map", map);
	}

	// 编辑备案号
	public ModelAndView recordCodeEdit(HttpServletRequest request,
			HttpServletResponse response) {
		Long id = ControllerHelper.extractId(request);
		Map map = new HashMap();
		map.put("code", getServiceContainer().getSettingService()
				.findRecordCodeById(id));
		map.put("appPrefix", getAppPrefix());
		return new ModelAndView("RecordCodeEdit.jsp", "map", map);
	}

	// 删除备案号
	public ModelAndView recordCodeDelete(HttpServletRequest request,
			HttpServletResponse response) {
		Long id = ControllerHelper.extractId(request);
		getServiceContainer().getSettingService().deleteRecordCode(id);
		return new ModelAndView(new RedirectView("RecordCodeSearch.do"));
	}

	// // ciquser
	// 创建ciq用户
	public ModelAndView cIQUserCreate(HttpServletRequest request,
			HttpServletResponse response) {
		UserLoginInfo r = new UserLoginInfo();
		Map map = new HashMap();
		r.setUserType("1");
		map.put("user", r);
		return new ModelAndView("CIQUserEdit.jsp", "map", map);
	}

	// 编辑ciq用户
	public ModelAndView cIQUserEdit(HttpServletRequest request,
			HttpServletResponse response) {
		Long id = ControllerHelper.extractId(request);
		Map map = new HashMap();
		map.put("user", getServiceContainer().getUserService().findCIQUserById(
				id));
		return new ModelAndView("CIQUserEdit.jsp", "map", map);
	}

	// 删除ciq用户
	public ModelAndView cIQUserDelete(HttpServletRequest request,
			HttpServletResponse response) {
		Long id = ControllerHelper.extractId(request);
		getServiceContainer().getUserService().deleteCIQUser(id);
		return new ModelAndView(new RedirectView("CIQUserSearch.do"));
	}

	// ciq用户权限
	public ModelAndView cIQUserAuthority(HttpServletRequest request,
			HttpServletResponse response) {
		Long id = ControllerHelper.extractId(request);
		Map map = new HashMap();
		UserLoginInfo user = getServiceContainer().getUserService()
				.findCIQUserById(id);
		List<UserRightsType> allRights = getServiceContainer().getUserService()
				.findAllRightType();
		List<UserRightSpec> userRights = getServiceContainer().getUserService()
				.findUserRightByUserid(user.getId());
		for (UserRightsType userRightsType : allRights) {
			for (UserRightSpec userRightSpec : userRights) {
				if (userRightsType.getCode().equals(
						userRightSpec.getRightCode())) {
					userRightsType.setChecked(true);
				}
			}
		}

		map.put("user", user);
		map.put("rights", allRights);
		return new ModelAndView("CIQUserAuthority.jsp", "map", map);
	}

	// //entuser
	// 创建企业用户
	public ModelAndView entUserCreate(HttpServletRequest request,
			HttpServletResponse response) {
		EntUserInfo r = new EntUserInfo();
		Map map = new HashMap();
		map.put("user", r);
		return new ModelAndView("EntUserEdit.jsp", "map", map);
	}

	// 编辑企业用户
	public ModelAndView entUserEdit(HttpServletRequest request,
			HttpServletResponse response) {
		Long id = ControllerHelper.extractId(request);
		Map map = new HashMap();
		map.put("user", getServiceContainer().getUserService().findEntUserById(
				id));
		return new ModelAndView("EntUserEdit.jsp", "map", map);
	}

	// 删除企业用户
	public ModelAndView entUserDelete(HttpServletRequest request,
			HttpServletResponse response) {
		Long id = ControllerHelper.extractId(request);
		getServiceContainer().getUserService().deleteEntUser(id);
		return new ModelAndView(new RedirectView("EntUserSearch.do"));
	}

	// common decl view
	private Map commonDeclView(HttpServletRequest request) {
		Long id;
		if (request.getParameter("id") != null) {
			// by decl id
			id = ControllerHelper.extractId(request);
		} else {
			// by goods id
			id = ControllerHelper.extractId(request, "gid");
			Goods goods = getServiceContainer().getCheckBizService()
					.findGoodsByGoodsId(id);
			Decl decl = getServiceContainer().getCheckBizService()
					.findDeclByDeclNo(goods.getDeclNo());
			id = decl.getId();
		}

		// 报检单
		Decl decl = getServiceContainer().getCheckBizService().findDeclById(id);
		// 集装箱
		List<Container> containerList = getServiceContainer()
				.getCheckBizService().findContainerByDecl(decl.getDeclNo());
		// 货物
		List<Goods> goodsList = getServiceContainer().getCheckBizService()
				.findGoodsByDeclNo(decl.getDeclNo());
		List<Goods> anchorGoodsList = new ArrayList<Goods>();
		List<Goods> nonAnchorGoodsList = new ArrayList<Goods>();
		for (Goods goods : goodsList) {
			goods.setWoodPackCheck(getServiceContainer().getCheckBizService()
					.findWoodPackCheckByDeclNoGoodsNoSynth(goods.getDeclNo(),
							goods.getGoodsNo()));
			if (goods.getAnchor() != null && goods.getAnchor().equals(1L)) {
				anchorGoodsList.add(goods);
			} else {
				nonAnchorGoodsList.add(goods);
			}
		}
		// 报检单详细查验信息
		InspResultSum inspResultSum = getServiceContainer()
				.getCheckBizService().findInspResultSumByDecl(decl.getDeclNo());
		// 集装箱结果
		List<ContainerCheck> containerCheckList = getServiceContainer()
				.getCheckBizService().findContainerCheckByDeclNo(
						decl.getDeclNo());
		// 集装箱疫情
		List<GoodsSituation> containerSituation = getServiceContainer()
				.getCheckBizService().findContainerSituationByDeclNo(
						decl.getDeclNo());

		// 监管结果
		if (decl.getType().equals("B")) {
			SupervisePlan supervisePlan = decl.getSupervisePlan();
			if (supervisePlan == null) {
				supervisePlan = getServiceContainer().getCheckBizService()
						.saveSupervisePlanByDeclNo(decl);
			}
			decl.setSupervisePlan(supervisePlan);
		}

		Map map = new HashMap();
		map.put("appPrefix", getAppPrefix());
		map.put("decl", decl);
		map.put("containerList", containerList);
		map.put("anchorGoodsList", anchorGoodsList);
		map.put("nonAnchorGoodsList", nonAnchorGoodsList);
		map.put("inspResultSum", inspResultSum);
		map.put("containerCheckList", containerCheckList);
		map.put("containerSituation", containerSituation);
		return map;
	}

	// normaldecl
	// 普货类报检单、货物详单
	public ModelAndView normalDeclView(HttpServletRequest request,
			HttpServletResponse response) {
		Map map = commonDeclView(request);
		return new ModelAndView("NormalDeclView.jsp", "map", map);
	}

	// maindecl
	// 展品类报检单、货物详单
	public ModelAndView mainDeclView(HttpServletRequest request,
			HttpServletResponse response) {
		Map map = commonDeclView(request);
		return new ModelAndView("MainDeclView.jsp", "map", map);
	}

	// 打开货物图片上传
	public ModelAndView goodsImgUpload(HttpServletRequest request,
			HttpServletResponse response) {
		Long id = ControllerHelper.extractId(request);
		List<DeclGoodsImg> files = getServiceContainer().getFileBizService()
				.findGoodsImgListByGoodsId(id);
		Map map = new HashMap<String, Object>();
		map.put("goodsId", id);
		map.put("files", files);
		return new ModelAndView("GoodsImgUpload.jsp", "map", map);
	}

	// 打开修改用户密码
	public ModelAndView userModifyPassword(HttpServletRequest request,
			HttpServletResponse response) {
		BaseUser user = ControllerHelper.getUserBySession();
		Map map = new HashMap();
		map.put("user", user);
		return new ModelAndView("UserModifyPassword.jsp", "map", map);
	}

	// ciq审核核销
	public ModelAndView auditClearList(HttpServletRequest request,
			HttpServletResponse response) {
		Long id = ControllerHelper.extractId(request);
		MainGoodsDecl decl = getServiceContainer().getCheckBizService()
				.findMainGoodsDeclById(id);
		List<MainDeclGoods> goodsList = getServiceContainer()
				.getCheckBizService().findClearGoodsByDeclNo(decl.getDeclNo());

		List<MainDeclGoods> applyClearlist = new ArrayList<MainDeclGoods>();
		List<MainDeclGoods> alreadyClearlist = new ArrayList<MainDeclGoods>();
		List<MainDeclGoods> noClearList = new ArrayList<MainDeclGoods>();
		for (MainDeclGoods goods : goodsList) {
			if (goods.getGoodsClearItem() == null
					|| goods.getGoodsClearItem().getClearFlag() == null
					|| goods.getGoodsClearItem().getClearFlag().intValue() < 4) {
				noClearList.add(goods);
			} else if (goods.getGoodsClearItem().getClearFlag().equals(4L)) {
				applyClearlist.add(goods);
			} else if (goods.getGoodsClearItem().getClearFlag().equals(5L)) {
				alreadyClearlist.add(goods);
			}
		}

		// noClearList.addAll(alreadyClearlist);

		Map map = new HashMap();
		map.put("decl", decl);
		map.put("noClearList", noClearList);
		map.put("applyClearlist", applyClearlist);
		map.put("alreadyClearlist", alreadyClearlist);
		map.put("appPrefix", getAppPrefix());
		return new ModelAndView("AuditClearList.jsp", "map", map);
	}

	// 货物详细查验结果
	public ModelAndView goodsCheckInfo(HttpServletRequest request,
			HttpServletResponse response) {
		Long goodsId = ControllerHelper.extractId(request, "goodsId");
		Map map = new HashMap();
		Goods goods = getServiceContainer().getCheckBizService()
				.findGoodsByGoodsId(goodsId);
		String declNo = goods.getDeclNo();
		Long goodsNo = goods.getGoodsNo();
		map.put("goods", goods);

		GoodsCheck goodsCheck = getServiceContainer().getCheckBizService()
				.findGoodsCheckByDeclNoGoodsNo(declNo, goodsNo);
		List<WoodPackCheck> woodList = getServiceContainer()
				.getCheckBizService().findWoodPackCheckByDeclNoGoodsNo(declNo,
						goodsNo);
		if (goodsNo.intValue() == 1) {
			woodList.addAll(getServiceContainer().getCheckBizService()
					.findWoodPackCheckByDeclNoGoodsNo(declNo, 0L));
		}
		List<GoodsSituation> poxList = getServiceContainer()
				.getCheckBizService().findGoodsSituationByDeclNoGoodsNo(declNo,
						goodsNo);

		map.put("goodsCheck", goodsCheck);
		map.put("woodList", woodList);
		map.put("poxList", poxList);

		return new ModelAndView("GoodsCheckInfo.jsp", "map", map);
	}

	// 根据通关单号取详细数据
	public ModelAndView releaseDeclView(HttpServletRequest request,
			HttpServletResponse response) {
		String releaseDeclNo = request.getParameter("releaseDeclNo");
		Map map = new HashMap();
		map.put("releaseDeclNo", releaseDeclNo);
		if (releaseDeclNo == null || releaseDeclNo.trim().length() == 0) {
			return new ModelAndView("ReleaseDeclView.jsp", "map", map);
		}

		ReleaseDecl releaseDecl = getServiceContainer().getCheckBizService()
				.updateReleaseDecl(releaseDeclNo);
		map.put("releaseDecl", releaseDecl);
		return new ModelAndView("ReleaseDeclView.jsp", "map", map);
	}

	// 打开人工获得报检单的页面
	public ModelAndView handGetDecl(HttpServletRequest request,
			HttpServletResponse response) {
		Map map = new HashMap();
		map.put("declPrep", null);
		return new ModelAndView("FetchDecl.jsp", "map", map);
	}

	// 货币汇率编辑
	public ModelAndView ccyRateEdit(HttpServletRequest request,
			HttpServletResponse response) {
		Long id = ControllerHelper.extractId(request);
		Map map = new HashMap();
		CcyRate ccyRate = getServiceContainer().getSettingService()
				.findCcyRateById(id);
		map.put("ccy", ccyRate);
		return new ModelAndView("CcyRateEdit.jsp", "map", map);
	}

	// 编辑疫情
	public ModelAndView epidemicNotifyEdit(HttpServletRequest request,
			HttpServletResponse response) {
		Long id = ControllerHelper.extractId(request);
		Map map = new HashMap();
		map.put("epidemicNotify", getServiceContainer().getSettingService()
				.findEpidemicNotifyById(id));
		return new ModelAndView("EpidemicNotifyEdit.jsp", "map", map);
	}

	// 创建疫情
	public ModelAndView epidemicNotifyCreate(HttpServletRequest request,
			HttpServletResponse response) {
		EpidemicNotify r = new EpidemicNotify();
		Map map = new HashMap();
		map.put("epidemicNotify", r);
		return new ModelAndView("EpidemicNotifyEdit.jsp", "map", map);
	}

	// 删除疫情
	public ModelAndView epidemicNotifyDelete(HttpServletRequest request,
			HttpServletResponse response) {
		Long id = ControllerHelper.extractId(request);
		getServiceContainer().getSettingService().deleteEpidemicNotify(id);
		return new ModelAndView(new RedirectView("EpidemicNotifySearch.do"));
	}

	// //////////////////////////////////////////////////////////
	public ModelAndView defaultView(HttpServletRequest request,
			HttpServletResponse response) {
		return new ModelAndView("");
	}
}
